package com.mu.controller;

import com.alibaba.fastjson.JSONObject;
import com.mu.service.MessageService;
import com.mu.util.RemoteHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.PushBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class HelloController {


    @Autowired
    private MessageService messageService;


    private String token = "shang";
    @GetMapping("hello/{name}")
    public String hello(@PathVariable("name")String name){
        return "Hello " + name + "!";
    }

    @GetMapping("send/{msg}")
    public String send(@PathVariable("msg")String msg){
        messageService.sendMessage("123", msg);
        return "success";
    }



//    @PostMapping("tencent/checkVerification")
//    public void checkVerification(HttpServletRequest request, HttpServletResponse response) {
//        System.out.println("开始校验签名...");
//        /**
//         * 接收微信服务器发送请求时传递过来的4个参数
//         */
//        //微信加密签名signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
//        String signature = request.getParameter("signature");
//        //时间戳
//        String timestamp = request.getParameter("timestamp");
//        //随机数
//        String nonce = request.getParameter("nonce");
//        //随机字符串
//        String echostr = request.getParameter("echostr");
//        System.out.println(timestamp+"---------"+nonce);
//        //排序
//        String sortString = sort(token, timestamp, nonce);
//
//        //加密
//        String mySignature = sha1(sortString);
//
//        //校验签名
////        if (mySignature != null && mySignature != "" && mySignature.equals(signature)) {
////            System.out.println("签名校验通过。");
////            //如果检验成功输出echostr，微信服务器接收到此输出，才会确认检验完成。
////            //response.getWriter().println(echostr);
////            try {
////                response.getWriter().write(echostr);
////            } catch (IOException e) {
////                e.printStackTrace();
////            }
////        } else {
////            System.out.println("签名校验失败。");
////        }
//
//
//        StringBuilder stringBuilder = new StringBuilder();
//        String line;
//
//        try {
//            BufferedReader reader = request.getReader();
//            while (null != (line = reader.readLine())){
//                stringBuilder.append(line);
//            }
//            log.info("stringBuilder:{}", stringBuilder.toString());
//
//            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
//            Document document = builderFactory.newDocumentBuilder().parse(stringBuilder.toString());
//            Element toUserName = document.getElementById("ToUserName");
//            try {
//                response.getWriter().write(echostr);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }catch (Exception e){
//            log.error("入参解析失败");
//        }
//
//    }
//
//
//    /**
//     * 加密的方法
//     * @param str  要加密的字符串
//     * @return 加密后的内容
//     */
//    public String sha1(String str)  {
//        try {
//            MessageDigest digest = MessageDigest.getInstance("SHA-1");
//            digest.update(str.getBytes());
//            byte messageDigest[] = digest.digest();
//            // Create Hex String
//            StringBuffer hexString = new StringBuffer();
//            // 字节数组转换为 十六进制 数
//            for (int i = 0; i < messageDigest.length; i++) {
//                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
//                if (shaHex.length() < 2) {
//                    hexString.append(0);
//                }
//                hexString.append(shaHex);
//            }
//            return hexString.toString();
//
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//        return "";
//    }
//
//    /**
//     * 排序方法
//     * @param token
//     * @param timestamp
//     * @param nonce
//     * @return
//     */
//    public String sort(String token, String timestamp, String nonce) {
//        String[] strArray = {token, timestamp, nonce};
//        Arrays.sort(strArray);
//        StringBuilder sb = new StringBuilder();
//        for (String str : strArray) {
//            sb.append(str);
//        }
//
//        return sb.toString();
//    }
//
//    public static void main(String[] args) {
//
//        String str = "<?xml version=\"1.0\" encoding=\"utf-8\"><xml><ToUserName><![CDATA[gh_fa9511a9bcd0]]></ToUserName><FromUserName><![CDATA[oLwMx1PmWKtcVQnEvdvQz1peHik0]]></FromUserName><CreateTime>1719570520</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[哈哈哈]]></Content><MsgId>24618329864978027</MsgId></xml>";
//        try {
//            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
//            Document document = builderFactory.newDocumentBuilder().parse(str);
//            Element toUserName = document.getElementById("xml");
//            log.info("toUserName:{}", JSONObject.toJSONString(toUserName));
//        }catch (Exception e){
//            log.error("失败", e);
//        }
//
//
//    }
}
